package app;

import common.struct.ActorInfo;
import common.struct.SensorData;
import common.struct.SensorInfo;
import common.struct.State;
import common.struct.ConstantEnum.CmdType;
import common.struct.ConstantEnum.SensorMode;

import java.util.Map;

public class AppDemo1 extends AbstractApp {
    @Override
    public void configApp() {
        this.appName = "Demo1";
        this.appDescription = "This is Demo1";
    }

    @Override
    public void getMsg(String sensorName, SensorData value) {
        // 因为注册了front传感器，所以每当front有新的数据时便会触发getMsg，
        // 其中channel是sensor名字，msg是sensor数据
        logger.info(String.format("[%s]: getMsg(sensorName, value) -> %s, %s", appName, sensorName, value));
        //用户代码
    }

    public static void main(String[] args) throws InterruptedException {
        AppDemo1 demo = new AppDemo1();
        AppRemoteConnector connector = AppRemoteConnector.getInstance();
        connector.connectPlatform("127.0.0.1", 9090);
        connector.registerApp(demo);

//        connector.getServiceInfo(ServiceType.CTX);
        Map<String, SensorInfo> supportedSensors = connector.getSupportedSensors();
        if (supportedSensors.containsKey("YellowCar") && supportedSensors.get("YellowCar").state == State.ON) {
            connector.registerSensor("YellowCar", SensorMode.PASSIVE, 1);
        }
        connector.getMsgThread(CmdType.START);
        Map<String, ActorInfo> supportedActors = connector.getSupportedActors();
        if (supportedActors.containsKey("YellowCar") && supportedActors.get("YellowCar").state == State.ON) {
            connector.registerActor("YellowCar");
        }
        connector.setActorCmd("YellowCar", "action_request");
//        while (true);
//        connector.dbset("myh1", "资源管理页面");
//        connector.dbget("myh1");
        Thread.sleep(5000);
        connector.unregisterApp(demo);
        connector.disConnectPlatform();
    }
}
